package com.yaozheng.vocationaltraining.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.ControlsUtils;
import com.yaozheng.vocationaltraining.view.question.BaseQuestionView;
import com.yaozheng.vocationaltraining.view.question.MultipleChoiceQuestionView_;
import com.yaozheng.vocationaltraining.view.question.SingleChoiceQuestionView_;
import com.yaozheng.vocationaltraining.view.question.TrueOrFalseQuestionView_;

/**
 * 一道题目
 * 
 * @author Administrator
 * 
 */
@EFragment(R.layout.item_fragment_question)
public class ItemQuestionWebViewFragment extends BaseFragment {
	// /**
	// * 单选题
	// */
	// SingleChoiceQuestionView singleChoiceQuestionView;
	// /**
	// * 多选题
	// */
	// MultipleChoiceQuestionView multipleChoiceQuestionView;
	// /**
	// * 判断
	// */
	// TrueOrFalseQuestionView trueOrFalseQuestionView;
	WebView questionContent;

	BaseQuestionView baseQuestionView;

	@ViewById
	FrameLayout questionContentOptionsFrame;
	int position;

	public void initWebView() {
		Bundle bundle = getArguments();
		position = bundle.getInt("position");
		questionContent.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		WebSettings webSettings = questionContent.getSettings();
		webSettings.setDefaultTextEncodingName("UTF-8");
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setJavaScriptEnabled(true);
		questionContent.addJavascriptInterface(this, "questionTypeService");
		questionContent.setVerticalScrollBarEnabled(true);
		questionContent.setBackgroundColor(Color.parseColor("#ffffff"));

		if (position % 3 == 0) {
			baseQuestionView = SingleChoiceQuestionView_
					.build(getBaseActivity());
		} else if (position % 3 == 1) {
			baseQuestionView = MultipleChoiceQuestionView_
					.build(getBaseActivity());
		} else {
			baseQuestionView = TrueOrFalseQuestionView_
					.build(getBaseActivity());
		}

		questionContentOptionsFrame.addView(baseQuestionView);
		setQuestionContentValue();
	}

	/**
	 * 给提纲
	 */
	public void setQuestionContentValue() {

		StringBuilder dataStr = new StringBuilder(
				"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><body ");
		if (android.os.Build.VERSION.SDK_INT > 10)
			dataStr.append("onload=\"javascript:questionTypeService.setQuestionContentSize(document.body.clientHeight);\"");
		dataStr.append(" style=\"background:#ffffff;font-size:20px; \">");
		dataStr.append("这个标志是何含义？" + position);
		// if (android.os.Build.VERSION.SDK_INT > 10)
		// dataStr.append("<br/><a style=\"cursor:pointer\" onclick=\"questionTypeService.openCalculator()\"><strong style=\"color: red\">计算器</strong></a>");
		dataStr.append("</body></html>");

		if (android.os.Build.VERSION.SDK_INT >= 21) {
			// android 5.0 需要调用两次，提纲才会变
			questionContent.loadData(dataStr.toString(),
					"text/html;charset=utf-8", null);
		}
		questionContent.loadData(dataStr.toString(), "text/html;charset=utf-8",
				null);
		if (android.os.Build.VERSION.SDK_INT <= 10) {
			// setQuestionSize(questionContent, 500);
		}
		questionContent.reload();

	}

	/**
	 * 设置提纲大小
	 * 
	 * @param height
	 *            高
	 */

	public void setQuestionContentSize(int height) {
		int[] sizes = ControlsUtils.getCurrScreenSize1(getActivity());
		int maxHeight = sizes[0] / 3;
		height = ControlsUtils.dip2px(getActivity(), height + 5);

		if (height > maxHeight) {
			height = maxHeight;
		}
		// setQuestionSizeHandler(height);
	}

	/**
	 * 设置提纲大小
	 * 
	 */
	@UiThread
	public void setQuestionSizeHandler(int height) {
		ViewGroup.LayoutParams layoutParams = questionContent.getLayoutParams();

		layoutParams.height = height;
		questionContent.setLayoutParams(layoutParams);
	}

	/**
	 * 设置提纲大小 android 10 以下
	 * 
	 * @param msg
	 *            消息对象
	 */
	@UiThread
	public void setQuestionSizeHandler10(int height) {
		ViewGroup.LayoutParams layoutParams = questionContent.getLayoutParams();
		layoutParams.height = questionContent.getContentHeight() + 60 > height ? height
				: questionContent.getContentHeight() + 60;
		questionContent.setLayoutParams(layoutParams);
	}

	/**
	 * 设置提纲大小
	 * 
	 * @param view
	 *            提纲
	 * @param delay
	 *            多少时间设置
	 */

	public void setQuestionSize(final View view, long delay) {
		questionContent.postDelayed(new Runnable() {
			@Override
			public void run() {
				int[] sizes = ControlsUtils.getCurrScreenSize1(getActivity());
				setQuestionSizeHandler10(sizes[0] / 3);
			}
		}, delay);
	}

}
